package com.bh.ly.oa.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.bh.ly.oa.GlobalData;
import com.bh.ly.oa.R;
import com.bh.ly.oa.adapter.OrderDetailPageAdater;
import com.bh.ly.oa.adapter.OrderListAdapter;
import com.bh.ly.oa.util.AsyncTaskFactory;
import com.bh.ly.oa.util.Config;
import com.bh.ly.oa.util.DateUtil;
import com.bh.ly.oa.util.HttpClent;
import com.bh.ly.oa.util.RequestListType;

public class OrderViewActivity extends BaseListActiivity {

	private TextView noticeTextView;
	private CountDownTimer timer;
    private GetUnreadOrderAsyncTask noticeTask;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_view);
		setURLTYPE(Config.newOderlist);
		  
		this.noticeTextView = (TextView) findViewById(R.id.notice_msg);
		if (GlobalData.getInstance().isHasUnreadOrder()) {
		    noticeTextView.setText(R.string.notice_unread_order_num);
            noticeTextView.setVisibility(View.VISIBLE);
        } else {
            noticeTextView.setVisibility(View.GONE);
        }
		
		timer = new CountDownTimer(Long.MAX_VALUE, 30000) {
            @Override
            public void onFinish() {
            }
            @Override
            public void onTick(long millisUntilFinished) {
                if (noticeTask != null) {
                    noticeTask.detachActivity();
                    noticeTask = null;
                }
                noticeTask = new GetUnreadOrderAsyncTask();
                noticeTask.attachActivity(OrderViewActivity.this);
                AsyncTaskFactory factory = new AsyncTaskFactory();
                factory.build().execute(noticeTask);
            }
        };
        timer.start();
        
		initDatas();
	}

	protected void initDatas() {
		loadingView = findViewById(R.id.loading_view);
		loadingFaildView = (TextView) findViewById(R.id.loading_fiald_view);
		ImageButton button = (ImageButton) findViewById(R.id.back_button);
		ImageButton rearch_button = (ImageButton) findViewById(R.id.rearch_button);
		button.setVisibility(View.VISIBLE);
		rearch_button.setVisibility(View.VISIBLE);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		rearch_button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent=new Intent(OrderViewActivity.this,SearchOrderViewActivity.class);
				startActivityForResult(intent, RequestListType.SEARCH_REQUEST_CODE_ORDER);
			}
		});
		listView= ((ListView) findViewById(R.id.order_item_list));
		m_footerView = LayoutInflater.from(this).inflate(R.layout.loading, null);
		listView.addFooterView(m_footerView);  
		adapter = new OrderListAdapter(this);
		((OrderListAdapter)adapter).setArray(array);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(OrderViewActivity.this, OrderDetailContentlistViewActivity.class);
				intent.putExtra("json", arg0.getAdapter().getItem(arg2) .toString());
				startActivity(intent);
			}
		});
		
		start();
	}
	
	@Override
	protected void start() {
		this.map.put("order_number", "");
		this.map.put("startdate", DateUtil.getMondayOFWeek());
		this.map.put("enddate", DateUtil.getCurrentWeekday());
		super.start();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(data == null) return;

		switch (requestCode) {
		case RequestListType.SEARCH_REQUEST_CODE_ORDER:
			array.clear();
			adapter.notifyDataSetChanged();
			pageNum = 1;
			map.put("currentPage", pageNum);
			String startDate = data.getStringExtra("startdate");
			String endDate = data.getStringExtra("enddate");
			this.map.put("startdate", startDate);
			this.map.put("enddate", endDate);
			this.map.put("order_number", data.getStringExtra("order_number") == null ? "" : data.getStringExtra("order_number"));
			super.start();
			break;
		default:
			break;
		}
	}
	
	private class GetUnreadOrderAsyncTask extends AsyncTask<String, Void, Boolean> {
        private Activity activity;

        public void attachActivity(Activity activity) {
            this.activity = activity;
        }

        public void detachActivity() {
            this.activity = null;
        }

        @Override
        protected Boolean doInBackground(String... arg0) {
            return HttpClent.hasUnreadOrderToday();
        }

        @Override
        protected void onPostExecute(Boolean code) {
            super.onPostExecute(code);

            GlobalData.getInstance().setHasUnreadOrder(code == null ? false : code);
            Log.d("GetUnreadOrderAsyncTask", "" + code);
            if (this.activity != null) {
                if (code != null && code) {
                    noticeTextView.setText(R.string.notice_unread_order_num);
                    noticeTextView.setVisibility(View.VISIBLE);
                } else {
                    noticeTextView.setVisibility(View.GONE);
                }
            }
        }
    }
}
